package com.jkl.nettyserver.Config;

import com.jkl.nettyserver.Properties.ZookeeperProperties;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZookeeperConfig {
    private final ZookeeperProperties zookeeperProperties;
    @Autowired
    public ZookeeperConfig(ZookeeperProperties zookeeperProperties) {
        this.zookeeperProperties = zookeeperProperties;
    }

    @Bean
    public CuratorFramework curatorFramework() {
        // 创建Curator客户端
        CuratorFramework client = CuratorFrameworkFactory.newClient(
                zookeeperProperties.getAddress(),// 配置ZooKeeper服务器的地址
                zookeeperProperties.getSessionTimeOutMs(),
                zookeeperProperties.getConnectTimeOutMs(),
                new ExponentialBackoffRetry(zookeeperProperties.getBaseSleepTimeMs(), zookeeperProperties.getMaxRetries()) // 设置重试策略
        );
        // 启动客户端
        client.start();
        return client;
    }
    public String getRootNode(){
        return zookeeperProperties.getRootNode();
    }
}
